package com.xxx.video.commen.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.xxx.video.commen.utils.DimensionUtil;

public class ProgressView extends LinearLayout {

    private Paint paint;
    private RectF rectF;
    private int progress;

    public ProgressView(Context context) {
        super(context);
        init(context);
    }


    public ProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        initPaint(context);
        setWillNotDraw(false);
    }

    private void initPaint(Context context) {
        paint = new Paint();
        paint.setColor(Color.parseColor("#30000000"));
        paint.setStyle(Paint.Style.FILL);
        rectF = new RectF();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int x = (getBottom() - getTop()) / 2;
        int y = (getRight() - getLeft()) / 2;
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.parseColor("#99000000"));
        canvas.drawCircle(x, y, DimensionUtil.dpToPx(20), paint);
        paint.setColor(Color.parseColor("#ffffff"));
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(x, y, DimensionUtil.dpToPx(20), paint);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.parseColor("#ffffff"));
        int right = getRight() - getLeft();
        int bottom = getBottom() - getTop();
        rectF.set(0, 0, right, bottom);
        int sweepAngle = progress * 36 / 10;
        canvas.drawArc(rectF, 0, sweepAngle, true, paint);
    }

    public void setProgress(int progress) {
        this.progress = progress;
        postInvalidate();
    }
}
